home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: uu4news.netcom.com!friend!news
- From: rich@kastle.com (Richard Krehbiel)
- Subject: Re: 12 bit datatype in binary file - misaligned
- Message-ID: <1996Mar25.122240.28678@friend.kastle.com>
- Sender: news@friend.kastle.com (News)
- Reply-To: rich@kastle.com
- Organization: Kastle Development Associates
- X-Newsreader: Forte Free Agent 1.0.82
- References: <4itqv9$kcq@news.ccit.arizona.edu>
- Date: Mon, 25 Mar 1996 12:21:16 GMT
-
- chrisl@SEDS.LPL.Arizona.EDU (Chris Lewicki) wrote:
-
- >Hello All,
- > I've been scouring the net and the bookstores for the last few
- >days trying to figure out how to read a 12-bit data length out of a
- >binary file.
- > Here's the setup: I have a data file that has all lengths of
- >fields in it. There are 1, 2, 3, 4, 6, 8, 12, and 16 bit data types
- >all packed in very efficiently. I've figured out that you can define
- >a structure:
-
- >struct BitData {
- > unsigned onebit : 1;
- > unsigned anotheronebit : 1;
- > unsigned fourbit : 4;
- > unsigned twobit : 2;
- > unsigned : 4; /* 4 bits padding */
- > unsigned char eightbit;
- > unsigned twelvebit : 12;
- > unsigned sixteenbit;
- >};
-
- >struct BitData bitdata;
-
- > Using this I could read in a file formatted this way with a simple
- >fread(&bitdata, ...) but I find that I encounter problems when I get
- >to the 12 bit data types. It will read in the first 12 bit variable
- >encountered, but everything after that is misaligned. I've searched
- >the world over, but I can't find a thing on this topic.
- > I understand that this is implementation dependent, and I am
- >using gcc 2.7.2 (and/or SUNWspro cc) on a Sparc 10 running Solaris
- >2.5. I've tried the Sun cc compiled using the -misalign compile flag,
- >but that doesn't work either.
-
- I think the "unsigned char eightbit;" is your problem. Did you want
- to say "unsigned eightbit : 8;" here? Just because char happens to be
- 8 bits does not mean a struct member of type char will be placed and
- aligned the same as a :8 bit field. A char member must be on an
- addressable boundary (a program may take it's address with the &
- operator), whereas an 8 bit field need not be so aligned.
-
- (And of course it's good that you know this is all implementation-
- dependent and how big a risk you are taking by depending on bit fields
- in external storage. I recall when Lattice C changed their bit field
- ordering and alignment rules between compiler versions.)
-
- --
- Richard Krehbiel, Kastle Systems, Arlington VA USA
- rich@kastle.com (work) or richk@mnsinc.com (personal)
-
-